<?php

class Users extends CActiveRecord {
	
	public function behaviors() {
		return array ('CRMTimeStampableBehavior' => array ('class' => 'littlecrm.db.behavior.CRMTimeStampableBehavior' ) );
	}
	
	public function updatePassword($password) {
		$this->_doHashPassword ( $password );
		return $this->update ();
	}
	
	protected function _doHashPassword($password) {
		$globalSalt = Yii::app ()->dbConfig->salt;
		$this->users_salt = $this->_generateSalt ();
		$this->users_password = sha1 ( $globalSalt . $password . $this->users_salt );
		$array = array ($this->users_salt, $this->users_password );
	}
	
	public function checkPassword($password) {
		$salt = Yii::app ()->dbConfig->salt;
		return sha1 ( $salt . $password . $this->users_salt ) === $this->users_password;
	}
	
	/**
	 * Returns the static model of the specified AR class.
	 * @return CActiveRecord the static model class
	 */
	public static function model($className = __CLASS__) {
		return parent::model ( $className );
	}
	
	public function findWithRoleAndProfile($id) {
		if (is_string ( $id )) {
			return $this->with ( 'profile', 'role' )->find ( 'LOWER(users_username)=?', array ($id ) );
		}
		return $this->with ( 'profile', 'role' )->find ( 'users_id=?', array ($id ) );
	}
	
	public function findByUsername($username) {
		return $this->find ( 'LOWER(users_username)=?', array ($username ) );
	}
	
	/**
	 * @return string the associated database table name
	 */
	public function tableName() {
		return 'users';
	}
	
	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules() {
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array (array ('users_username, users_password, users_email, users_salt, users_status_code, users_created_at, users_changed_at', 'required' ), array ('users_status_code', 'numerical', 'integerOnly' => true ), array ('users_username', 'length', 'max' => 45 ), array ('users_password', 'length', 'max' => 40 ), array ('users_email', 'email' ), array ('users_firstname', 'length', 'min' => 3, 'max' => 50 ), array ('users_lastname', 'length', 'min' => 3, 'max' => 50 ) );
	}
	
	/**
	 * @return array relational rules.
	 */
	public function relations() {
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'phones' => array(self::BELONGS_TO, 'Phones', 'users_phone_id'),
		);
	}
	
	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels() {
		return array ('users_username' => Yii::t ( 'crm', 'Nutzername' ), 'users_password' => Yii::t ( 'crm', 'Passwort' ), 'users_email' => Yii::t ( 'crm', 'E-Mail-Adresse' ), 'users_salt' => Yii::t ( 'crm', 'Vergleichs Hash' ), 'users_status_code' => Yii::t ( 'crm', 'Status' ), 'users_created_at' => Yii::t ( 'crm', 'Erstellt' ), 'users_changed_at' => Yii::t ( 'crm', 'Geändert' ), 'users_firstname' => Yii::t ( 'crm', 'Vorname' ), 'users_lastname' => Yii::t ( 'crm', 'Nachname' ) );
	}
	
	protected function _generateSalt() {
		$dynamic_salt = '';
		mt_srand ( microtime () * 1000000 );
		for($i = 0; $i < 32; ++ $i) {
			$dynamic_salt .= chr ( mt_rand ( 33, 126 ) );
		}
		$dynamic_salt = str_replace ( "'", '$', $dynamic_salt );
		return $dynamic_salt;
	}

}